package com.yg.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yg.config.result.R;
import com.yg.config.result.ResultCode;
import com.yg.entity.readAndWriteTemplates.TeacherAppointTemplate;
import com.yg.entity.singleEntity.Teacher;
import com.yg.entity.singleEntity.TeacherAppoint;
import com.yg.service.TeacherAppointService;
import com.yg.service.TeacherService;
import com.yg.utils.DateValidationUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class TeacherAppointReadListener extends AnalysisEventListener<TeacherAppointTemplate> {


    private TeacherAppointService teacherAppointService;
    private TeacherService teacherService;
    private R r;


    public TeacherAppointReadListener(TeacherAppointService teacherAppointService, TeacherService teacherService, R r) {
        this.teacherAppointService = teacherAppointService;
        this.teacherService = teacherService;
        this.r = r;
    }

    //表格信息
    private List<TeacherAppointTemplate> list = new ArrayList<>();

    //错误信息
    private List<String> errorList = new ArrayList<>();

    @Override
    public void invoke(TeacherAppointTemplate teacherAppointTemplate, AnalysisContext analysisContext) {
        System.out.println(teacherAppointTemplate.toString());
        list.add(teacherAppointTemplate);
    }

    /**
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        list.forEach(e -> {
            String teacherId = e.getTeacherId();
            String teacherName = e.getName();

            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

            wrapper.eq("teacher_id", teacherId);
            wrapper.eq("name", teacherName);

            Teacher one = null;
            try {
                one = teacherService.getOne(wrapper);
                System.out.println("one：" + one.toString());
            } catch (Exception exception) {
                exception.printStackTrace();
                System.out.println("此教师信息错误：" + e.toString());
                errorList.add("此教师信息错误：" + e.toString());
            }

            List<TeacherAppoint> teacherAppointList = new ArrayList<>();
            if (one != null) {
                TeacherAppoint teacherAppoint = new TeacherAppoint();
                BeanUtils.copyProperties(e, teacherAppoint);
                teacherAppoint.setId(one.getId());
                teacherAppoint.setStartTime(DateValidationUtil.dateValidation(teacherAppoint.getStartTime(),"yyyy.MM"));
                teacherAppoint.setEndTime(DateValidationUtil.dateValidation(teacherAppoint.getEndTime(),"yyyy.MM"));
                System.out.println("teacherAppoint：" + teacherAppoint.toString());
                System.out.println("e：" + e.toString());

                teacherAppointList.add(teacherAppoint);
            } else {
                System.out.println("没有查询到此教师：" + e.toString());
                errorList.add("没有查询到此教师：" + e.toString());
            }

            try {
                //假如没有报错，则将数据存入到数据库中
                if (errorList.isEmpty()) {
                    //存入到数据库中
                    teacherAppointService.saveBatch(teacherAppointList);
                } else {
                    r.success(false);
                    r.code(ResultCode.IMPORT_TABLE_ERROR_MESSAGE).message(errorList.toString());
                }
            } catch (Exception exception) {
                exception.printStackTrace();
                r.success(false);
                r.code(ResultCode.IMPORT_TABLE_EXCEPTION).message(errorList.toString());
            }
        });
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        System.out.println("错误：" + exception.getMessage());
        errorList.add("错误：" + exception.getMessage());
    }
}
